import { Module, NestModule, MiddlewareConsumer } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
// 模块
import { AuthModule } from './auth/auth.module';
import { UploadModule } from './upload/upload.module';
// 跨域
import { globalCors } from './middleware/cors.midd';
// 注册jwt为全局守卫
import { APP_GUARD } from '@nestjs/core';
import { jwtAuth } from './auth/jwt-auth.grard';
// 导入角色守卫
import { RolesGuard } from './auth/roles.guard'; 
// 静态资源访问
import { ServeStaticModule } from '@nestjs/serve-static';
import { join } from 'path'
import { ClassroomModule } from './one-to-many/classroom.module';
import { StudentModule } from './many-to-many/student.module';
// websocket
import { WsStartGateway } from './websocket/ws.gateway';
// mysql
import { DataBase } from './config/database.';
import { TypeOrmModule } from '@nestjs/typeorm';
@Module({
  imports: [
    AuthModule,
    TypeOrmModule.forRoot(DataBase),
    ServeStaticModule.forRoot({
      rootPath: join(__dirname, '..', 'uploads'), // 指定静态文件的根目录
      serveRoot: '/joshua/', // 指定URL前缀
    }),
    UploadModule,
    ClassroomModule,
    StudentModule,
  ],
  controllers: [AppController],
  // 注册为全局守卫
  providers: [
    AppService, 
    {
      provide: APP_GUARD,
      useClass: jwtAuth,
    },{
      provide: APP_GUARD,
      useClass: RolesGuard
    },
    WsStartGateway
  ],
})
export class AppModule implements NestModule {
  configure(consumer: MiddlewareConsumer) {
    consumer.apply(globalCors).forRoutes('*'); // 应用到所有路由
  }
}
